package com.theme.passport.controller.theme;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.view.R;
import com.theme.passport.entity.constant.ThemeAttchmentTypeEnum;
import com.theme.passport.entity.theme.ThemeAttachmentEntity;
import com.theme.passport.entity.theme.WidgeWeatherImagesEntity;
import com.theme.passport.entity.theme.WidgeConfigEntity;
import com.theme.passport.service.theme.WidgeConfigService;
import com.theme.passport.service.theme.WidgeWeatherImagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 组件配置表
 *
 * @author live
 * @email 870459550@qq.com
 * @date 2022-07-18 09:19:12
 */
@RestController
@RequestMapping("widge/widge-config")
public class WidgeConfigController {

    @Autowired
    private WidgeConfigService widgeConfigService;

    @Autowired
    private WidgeWeatherImagesService widgeWeatherImagesService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("widge:widge-config:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = widgeConfigService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("widge:widge-config:info")
    public R info(@PathVariable("id") Long id){
        WidgeConfigEntity tabWidgeConfig = widgeConfigService.getById(id);

        return R.ok().put("widgeConfig", tabWidgeConfig);
    }

    /**
     * 信息
     */
    @RequestMapping("/queryInfoByWidgeId")
    //@RequiresPermissions("widge:widge-config:info")
    public R queryInfoByWidgeId(@RequestParam("widgeId") Long widgeId){

        WidgeConfigEntity tabWidgeConfig = widgeConfigService.queryInfoByWidgeId(widgeId);

        return R.ok().put("widgeConfig", tabWidgeConfig);
    }

    /**
     * 信息
     */
    @RequestMapping("/queryWidgeConfigListByWidgeId")
    //@RequiresPermissions("widge:widge-config:info")
    public R queryWidgeConfigListByWidgeId(@RequestParam("widgeId") Long widgeId){

        List<WidgeConfigEntity> widgeConfigList = widgeConfigService.queryWidgeConfigList(widgeId);

        return R.ok().put("widgeConfigList", widgeConfigList);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("widge:widge-config:save")
    public R save(@RequestBody WidgeConfigEntity tabWidgeConfig){
        tabWidgeConfig.setCreated(new Date());
        tabWidgeConfig.setUpdated(new Date());
        widgeConfigService.save(tabWidgeConfig);
        //存天气组图图片
        List<String> imageList = tabWidgeConfig.getWeatherImages();
        if (null != imageList && imageList.size() > 0){
            List<WidgeWeatherImagesEntity> listAttachment = new ArrayList<>();
            imageList = imageList.stream().distinct().collect(Collectors.toList());
            imageList.forEach(image ->{
                WidgeWeatherImagesEntity tabWeatherImages = new WidgeWeatherImagesEntity();
                tabWeatherImages.setWidgeConfigId(tabWidgeConfig.getId());
                tabWeatherImages.setFileUrl(image);
                tabWeatherImages.setCreated(new Date());
                tabWeatherImages.setUpdated(new Date());
                listAttachment.add(tabWeatherImages);
            });
            widgeWeatherImagesService.saveBatch(listAttachment);
        }

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("widge:tabwidgeconfig:update")
    public R update(@RequestBody WidgeConfigEntity tabWidgeConfig){
        tabWidgeConfig.setUpdated(new Date());
        widgeConfigService.updateById(tabWidgeConfig);



        //存天气组图图片
        List<String> imageList = tabWidgeConfig.getWeatherImages();
        if (null != imageList && imageList.size() > 0){
            //删除旧的天气组图
            widgeWeatherImagesService.remove(Wrappers.<WidgeWeatherImagesEntity>query().lambda()
                     .eq(WidgeWeatherImagesEntity::getWidgeConfigId, tabWidgeConfig.getId()));

            List<WidgeWeatherImagesEntity> listAttachment = new ArrayList<>();
            imageList = imageList.stream().distinct().collect(Collectors.toList());
            imageList.forEach(image ->{
                WidgeWeatherImagesEntity tabWeatherImages = new WidgeWeatherImagesEntity();
                tabWeatherImages.setWidgeConfigId(tabWidgeConfig.getId());
                tabWeatherImages.setFileUrl(image);
                tabWeatherImages.setCreated(new Date());
                tabWeatherImages.setUpdated(new Date());
                listAttachment.add(tabWeatherImages);
            });
            widgeWeatherImagesService.saveBatch(listAttachment);
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("widge:widge-config:delete")
    public R delete(@RequestBody Long[] ids) {
        widgeConfigService.removeByIds(Arrays.asList(ids));
        //删除天气组图
        for (Long id : ids) {
            widgeWeatherImagesService.remove(Wrappers.<WidgeWeatherImagesEntity>query().lambda()
                    .eq(WidgeWeatherImagesEntity::getWidgeConfigId, id));
        }

        return R.ok();
    }

}
